import numpy as np
import array
from concurrent.futures import ProcessPoolExecutor

from meta import LOG, LogMixin, MAX_PROCESS_NUM, TimerMixin
from game.token import Tokens
from game.object import Coin, MemoryCoin


class CoinTest(LogMixin, TimerMixin):
    def __init__(self, num, CoinClass=Coin):
        super().__init__()
        self.coin = CoinClass(num)
        self.results = array.array('I', [])

    def test_flip(self):
        """测试硬币抛硬币"""
        self.results.append(self.coin.flip())

    def test_flips(self, num):
        """测试硬币迭代抛硬币"""
        self.timer.mark(f'测试硬币迭代抛硬币')

        # 多进程调用
        for i in self.coin.flips(num):
            self.results.append(i)

        # 单进程方法
        # self.results.extend(self.coin.flip(num))
            
        self.__log_std()

        
        self.timer.log()

    def __log_std(self):
        self.logger.warning(f'共抛出{len(self.results)}次')
        self.logger.warning(f'均一化标准差为{np.std(self.results) / self.coin.num}')

if __name__ == '__main__':

    # 测试硬币抛硬币
    test = CoinTest(num=10, CoinClass=Coin)
    
    test.test_flips(num=100)
    
    # LOG.info(f'硬币抛硬币结果：{test.results}')

    # # 测试代币池
    # vault1 = Tokens()
    # vault1.gen(num=100001)
    # vault2 = vault1 - 10000
    # vault2 += vault1
    # print(vault1)
    # print(vault2)

    # vault1 += vault2
    # print(vault1)
    # print(vault2)
